/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stylefeng.sso.server.constants.factory;


import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.stylefeng.sso.server.constants.cache.Cache;
import com.stylefeng.sso.server.constants.cache.CacheKey;
import com.stylefeng.sso.server.modular.entity.Role;
import com.stylefeng.sso.server.modular.mapper.RoleMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * 快捷查询方法
 *
 * @author fengshuonan
 * @date 2017年2月13日 下午10:55:21
 */
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {

    private RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);
//    private DeptMapper deptMapper = SpringContextHolder.getBean(DeptMapper.class);
//    private DictMapper dictMapper = SpringContextHolder.getBean(DictMapper.class);
//    private DictTypeMapper dictTypeMapper = SpringContextHolder.getBean(DictTypeMapper.class);
//    private UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
//    private MenuMapper menuMapper = SpringContextHolder.getBean(MenuMapper.class);
//    private NoticeMapper noticeMapper = SpringContextHolder.getBean(NoticeMapper.class);
//    private UserPosService userPosService = SpringContextHolder.getBean(UserPosService.class);
//    private PositionService positionService = SpringContextHolder.getBean(PositionService.class);

    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }





    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId")
    public String getSingleRoleName(Long roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleMapper.selectById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getName();
        }
        return "";
    }

    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_TIP + "'+#roleId")
    public String getSingleRoleTip(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleMapper.selectById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getDescription();
        }
        return "";
    }

}
